<?php

function curl_get_image($url, $file){
    $ch = curl_init($url);
    if($ch){
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_FILE, $file);
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 5.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1");
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        curl_setopt($ch, CURLOPT_PROXY, '69.163.96.3:8080');
        curl_exec($ch);
    }
    curl_close($ch);
}

ini_set('max_execution_time','0');
ini_set('error_reporting', E_ALL);
$config = array(
        'IN' => 'H:/parse/out',
        //'TEMPLATE' => '<div class="breadcrumbs">#breadcrumbs#</div><h2>#title#</h2><div id="content">#description#</div><div id="links">#links#</div>',
        'OUT' => 'H:/parse/res'
);
$outputImagesDir = $config['OUT'].'/images'; 
if(!mkdir($outputImagesDir)){
    if (!file_exists($outputImagesDir)){
        echo 'Error making images directory<br><br>';
        exit;
    }
};
for($i = 1; $i < 11; $i++){
    $fileName = $config['IN'].'/'.$i.'.txt';
    $content = file_get_contents($fileName);
    preg_match('@(class="highslide" onclick="return hs.expand\(this\)" style="float:left; margin: 10px">([\d|\D]*)<tr><td colspan="2"><hr></td></tr></table>)@', $content, $matches);
    $description = $matches[2];
    preg_match('@src="([^\"]*)"@', $description, $matches);
    $remoteImgSrc = $matches[1];
     preg_match('@(.*)\/(.*)\.jpg@' , $remoteImgSrc, $matches);
    $localImageFileName = $matches[2].'.jpg';
    $localImageFile = fopen($outputImagesDir.'/'.$localImageFileName, 'wb');
    echo 'Processing '.$localImageFileName.'<br><br>';
    if($localImageFile) curl_get_image($remoteImgSrc, $localImageFile);
    else echo 'Error maling image copy <br><br>';
    echo '<br><br>';
}
?>